<?php
class ProductController extends AppController {
	var $name = 'Product';
	var $helpers = array();
	var $components = array('Menu','Init');

	function beforeFilter() {
		$this->Init->startup($this);
		$this->Init->init();
		$this->Init->getKeyword();
		$_session = $this -> Session -> read();
		$this->loadModel('Hitcounter');
		$row = $this->Hitcounter->get(session_id());
		if ($row == 0) {
			$data = array(
				"session_id" => session_id(),
				"ref_url" => env("REQUEST_URI"),
				"ip_address" => env("REMOTE_ADDR"),
				"activity" => date("Y-m-d H:i:s")
			);
			$this->Hitcounter->save($data);
		}
		$this->set('activeNav','Product');
		Configure::write("Config.language", 'vie');
		$this->set('lang','vie');
                $this->set('vipDiscount', $this->Session->read('discount_for_vip'));
	}

	function index($id = null) {
		$this->layout = 'inside';
		$this->loadModel('Product');

		$this->paginate = array(
			'Product' => array(
				'conditions' =>  array('Product.status' => 1),
				'order' => array('Product.updated_date desc'),
				'limit' => 16
		));
		$this->set('products', $this->paginate('Product'));
	}

	function view($id = NULL) {
		$this->layout = 'inside';
		$this->loadModel('ProductType');
		$this->loadModel('Product');

		if($id > 0) {
			$this->loadModel('ProductType');
			$category = $this->ProductType->get($id);
			$cat = $this->ProductType->getTree($id);
			$this->paginate = array(
				'Product' => array(
					'conditions'	=> array(
								'Product.type'=> $cat,
								'Product.status'=> 1
							),
					'limit' => 16,
					'order'		=> 'Product.date desc'
				)
			);
			$this->set('products', $this->paginate('Product'));
			$_cat_name = $this->ProductType->find('first', array(
				'conditions' => array('ProductType.id'=> $id)
			));

			$this->set('cat_name2', $_cat_name['ProductType']['name_vie']);
			$parent = $_cat_name['ProductType']['parent'];
                        
		}

		else {
			$category = array();
			$this->paginate = array(
				'Product' => array(
					'conditions'	=> array(
						'Product.status'=> 1
							),
					'limit' => 16,
					'order'		=> 'Product.date desc'
				)
			);

			$this->set('products', $this->paginate('Product'));
			$parent = 1;
		}

                $_cat_name = $this->ProductType->find('first', array(
                    'conditions' => array('ProductType.id'=> $parent)
                ));
                
		$this->set('cat_name', $_cat_name['ProductType']['name_vie']);
		$this->set('category',$category);
		
	}

	function detail($id = NULL) {
		$this->layout = 'inside';
		$this->loadModel('Product');
		$this->loadModel('ProductType');

		if ($id > 0) {
			$products = $this->Product->find('first', array(
				'conditions' =>  array('Product.id' => $id)
			));
			$this->set('products', $products);

			$_cat_name = $this->ProductType->find('first', array(
				'conditions' => array('ProductType.id'=> $products['Product']['type'])
			));

			$this->set('cat_name2', $_cat_name['ProductType']['name_vie']);

			$_cat_name = $this->ProductType->find('first', array(
				'conditions' => array('ProductType.id'=> $_cat_name['ProductType']['parent'])
			));

			$this->set('cat_name', $_cat_name['ProductType']['name_vie']);
			// product img
			$this->loadModel('ProductImg');
			$product_img = $this->ProductImg->find('all',array(
				'conditions' => array(
					'ProductImg.product' => $id,
					'ProductImg.status' => 1
				)
			));

			$this->set('product_img',$product_img);
		}
		else {
			$this->redirect('/product');
		}
	}

	function order($id = null, $action = 'index') {

		$this->layout = 'inside';
		if (intval($id) != null) {
			$order = $this->Session->read('User.order');
			$this->loadModel('Product');
			$product = $this->Product->find('first', array(
				'conditions' =>  array('Product.id' => intval($id))
			));
			if(!empty($product)) {
				if(isset($order[intval($id)])) {
					$order[intval($id)]['quantity'] +=
						1;
				} else {
					//Set id
					$order[intval($id)]['id'] =
						$product[$this->Product->alias]['id'];

					//Set quantity
					$order[intval($id)]['quantity'] =
						1;
                                        
					//Set cost
					$order[intval($id)]['price'] =
						$product[$this->Product->alias]['price'];

					//Set name
					$order[intval($id)]['name_eng'] =
						$product[$this->Product->alias]['name_eng'];

                                        
					$order[intval($id)]['name_vie'] =
						$product[$this->Product->alias]['name_vie'];

				}

				$this->Session->write('User.order', $order);

				$this->Session->setFlash(__('This product was added successfully.', true));
				@$this->redirect(array('controller'=>'product', 'action' => 'index', $product[$this->Product->alias]['type']));
			}
		}
		if ($action == 'detail') {
			$this->redirect(array('controller'=>'product', 'action' => 'detail',$id));
		}
		else {
			$this->redirect(array('controller'=>'product', 'action' => 'index',$product['Product']['type']));
		}
		$this->render('index');

	}

	function basket() {
		$this->layout = 'inside';
		$order = $this->Session->read('User.order');
		$this->set('orders', $order);
	}
}
?>
